#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include "cmdfifo.h"
#include "object.h"
#include "at_service.h"
#include "cssl/cssl.h"
/*#include "at_base.h"*/

void signal_handler(int num);



int tty = 0;
struct termios newt, oldt;
#define     DEVICE   "/dev/ttyUSB2"
cssl_t *serial = NULL;
int main()
{
    signal(SIGINT, signal_handler);

    cssl_start ();    //串口准备
    serial = cssl_open (DEVICE,NULL, 1, 9600, 8, 1, 1);//打开串口
    if ( NULL == serial)
    {
        perror ("open "DEVICE);
        exit (EXIT_FAILURE);
    }

    /*while(1)*/
    {
    cssl_putstring (serial, "+CMT: \"\",30\r\n");
    usleep(10000);
    cssl_putstring (serial, "0891683108200455F6240D91688196888558F60000511082116571230BE3335A8D2693F368B319\r\n");
    /*cssl_putstring (serial, "0891683108200455F6240EA1212560058695990008412181611553230E5F8B946B003A0020003100320033\r\n");*/
    /*usleep(100);*/
    }

    /*exit(0);*/
    at_service_init ();

    int data;
    while (1)
    {
        data = cssl_getchar(serial);
        at_cmdline_get_char (data);
        putchar (data);
        if (data == 0x0d)
        {
            at_cmdline_get_char (0x0a);
        }
        /*printf ("[input]%02X ", data);*/
        at_service_main_loop ();
    }

    cssl_close (serial);
    cssl_stop ();
    exit (EXIT_SUCCESS);
}

/*void ( *signal(int signum, void (*handler)(int)) ) (int);*/
void signal_handler(int num)
{
    if (num == SIGINT)
    {
        printf ("程序终止！\n");
        at_service_destroy ();
        cssl_close (serial);
        cssl_stop ();
        exit (EXIT_SUCCESS);
    }
}
